home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Przegladarki internetowe / Mozilla Seamonkey 1.0.5 pl / seamonkey-1.0.5.pl-PL.win32.installer.exe / BROWSER.XPI / bin / chrome / toolkit.jar / content / global / nsWidgetStateManager.js < prev    next >
Encoding:
JavaScript  |  2004-10-11  |  16.0 KB  |  479 lines

  1. /* -*- Mode: Java; tab-width: 2; c-basic-offset: 2; -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is mozilla.org Code.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Ben Goodger <ben@netscape.com> (Original Author)
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /** Presenting widgetStateManager the Third.
  41.  *   a production of ye olde bard Ben Goodger and his Merry XUL Widget Crewe.
  42.  *  =>> MODIFICATIONS MUST BE REVIEWED BY ben@netscape.com!!! <<=
  43.  **/
  44.  
  45. var wsm;
  46.  
  47. function nsWidgetStateManager ( aFrameID )
  48.   {
  49.  
  50.     this.dataManager =
  51.       {
  52.         /** Persisted Data Hash Table
  53.          *  Page_ID -> Element_ID -> Property -> Value
  54.          **/
  55.         pageData: [],
  56.  
  57.         setPageData:
  58.           function ( aPageTag, aDataObject )
  59.             {
  60.               this.pageData[aPageTag] = aDataObject;
  61.             },
  62.  
  63.         getPageData:
  64.           function ( aPageTag )
  65.             {
  66.               if( !(aPageTag in this.pageData) )
  67.                 this.pageData[aPageTag] = [];
  68.               return this.pageData[aPageTag];
  69.             },
  70.  
  71.         setItemData:
  72.           function ( aPageTag, aItemID, aDataObject )
  73.             {
  74.               if( !(aPageTag in this.pageData) )
  75.                 this.pageData[aPageTag] = [];
  76.               this.pageData[aPageTag][aItemID] = aDataObject;
  77.             },
  78.  
  79.         getItemData:
  80.           function ( aPageTag, aItemID )
  81.             {
  82.               if( !(aItemID in this.pageData[aPageTag]) )
  83.                 this.pageData[aPageTag][aItemID] = [];
  84.               return this.pageData[aPageTag][aItemID];
  85.             }
  86.       }
  87.  
  88.     this.contentID    = aFrameID;
  89.  
  90.     wsm               = this;
  91.  
  92.     /** Element Handlers
  93.      *  Provides default get and set handler functions for supported
  94.      *  widgets. Clients can override or add new widgets.
  95.      **/
  96.     this.handlers     =
  97.       {
  98.         colorpicker:
  99.           {  get: wsm.get_Colorpicker, set: wsm.set_Colorpicker   },
  100.         menulist:
  101.           {  get: wsm.get_Menulist,    set: wsm.set_Menulist      },
  102.         radiogroup:
  103.           {  get: wsm.get_Radiogroup,  set: wsm.set_Radiogroup    },
  104.         checkbox:
  105.           {  get: wsm.get_Checkbox,    set: wsm.set_Checkbox      },
  106.         textbox:
  107.           {  get: wsm.get_Textbox,     set: wsm.set_Textbox       },
  108.         listitem:
  109.           {  get: wsm.get_Listitem,    set: wsm.set_Listitem      },
  110.         data:
  111.           {  get: wsm.get_Data,        set: wsm.set_Data          },
  112.         default_handler:
  113.           {  get: wsm.get_Default,     set: wsm.set_Default       }
  114.       }
  115.  
  116.     // extra attributes to scan and save.
  117.     this.attributes   = [];
  118.   }
  119.  
  120. nsWidgetStateManager.prototype =
  121.   {
  122.     get contentArea()
  123.       {
  124.         return window.frames[ this.contentID ];
  125.       },
  126.  
  127.     savePageData:
  128.       function ( aPageTag )
  129.         {
  130.           if (!(aPageTag in this.dataManager.pageData))
  131.             return;
  132.  
  133.  
  134.             // Automatic element retrieval. This is done in two ways.
  135.             // 1) if an element id array is present in the document, this is
  136.             //    used to build a list of elements to persist. <-- performant
  137.             // 2) otherwise, all elements with 'wsm_persist' set to true
  138.             //    are persisted <-- non-performant.
  139.            var elements;
  140.            if( '_elementIDs' in this.contentArea )
  141.               {
  142.                 elements = [];
  143.                 for( var i = 0; i < this.contentArea._elementIDs.length; i++ )
  144.                   {
  145.                     var elt = this.contentArea.document.getElementById( this.contentArea._elementIDs[i] );
  146.                     if (elt) {
  147.                       elements[elements.length] = elt;
  148.                     } else {
  149.                       // see bug #40329. People forget this too often, and it breaks Prefs
  150.                       dump("*** FIX ME: '_elementIDs' in '" + this.contentArea.location.href.split('/').pop() +
  151.                            "' contains a reference to a non-existent element ID '" +
  152.                            this.contentArea._elementIDs[i] + "'.\n");
  153.                     }
  154.                   }
  155.               }
  156.             else
  157.               {
  158.                 elements = this.contentArea.document.getElementsByAttribute( "wsm_persist", "true" );
  159.               }
  160.             for( var ii = 0; ii < elements.length; ii++ )
  161.               {
  162.                 var elementID   = elements[ii].id;
  163.                 var elementType = elements[ii].localName;
  164.                 if (!(aPageTag in this.dataManager.pageData) )
  165.                     this.dataManager.pageData[aPageTag] = [];
  166.                 this.dataManager.pageData[aPageTag][elementID] = [];
  167.                 // persist attributes
  168.                 var get_Func = (elementType in this.handlers) ?
  169.                                 this.handlers[elementType].get :
  170.                                 this.handlers.default_handler.get;
  171.                 this.dataManager.setItemData( aPageTag, elementID, get_Func( elementID ) );
  172.               }
  173.            if( 'GetFields' in this.contentArea)
  174.              {
  175.                // save page data based on user supplied function in content area
  176.                var dataObject = this.dataManager.getPageData( aPageTag );
  177.                dataObject = this.contentArea.GetFields( dataObject );
  178.                if (dataObject)
  179.                  this.dataManager.setPageData( aPageTag, dataObject );
  180.              }
  181.         },
  182.  
  183.     setPageData:
  184.       function ( aPageTag )
  185.         {
  186.           var pageData = this.dataManager.getPageData( aPageTag );
  187.           if( 'SetFields' in this.contentArea )
  188.             {
  189.               if ( !this.contentArea.SetFields( pageData ) )
  190.               {
  191.                 // If the function returns false (or null/undefined) then it
  192.                 // doesn't want *us* to process the page data.
  193.                 return;
  194.               }
  195.             }
  196.  
  197.           for( var elementID in pageData )
  198.             {
  199.               var element = this.contentArea.document.getElementById( elementID );
  200.               if( element )
  201.                 {
  202.                   var elementType = element.localName;
  203.                   var set_Func = (elementType in this.handlers) ?
  204.                                   this.handlers[elementType].set :
  205.                                   this.handlers.default_handler.set;
  206.                   set_Func( elementID, pageData[elementID] );
  207.                 }
  208.             }
  209.         },
  210.  
  211.  
  212.     /** Widget Get/Set Function Implementations
  213.      *  These can be overridden by the client.
  214.      **/
  215.     generic_Set:
  216.       function ( aElement, aDataObject )
  217.         {
  218.           if( aElement )
  219.             {
  220.               for( var property in aDataObject )
  221.                 {
  222.                   if (property == "localname")
  223.                     continue;
  224.                   if ( !aDataObject[property] && typeof aDataObject[property] == "boolean")
  225.                     aElement.removeAttribute( property );
  226.                   else
  227.                     aElement.setAttribute( property, aDataObject[property] );
  228.                 }
  229.               if ( !aElement.getAttribute("disabled","true") )
  230.                 aElement.removeAttribute("disabled");
  231.             }
  232.         },
  233.  
  234.     generic_Get:
  235.       function ( aElement )
  236.         {
  237.           if( aElement )
  238.             {
  239.               var dataObject = [];
  240.               var wsmAttributes = aElement.getAttribute( "wsm_attributes" );
  241.               var attributes = wsm.attributes;              // make a copy
  242.               if( wsmAttributes != "" )
  243.                 {
  244.                   attributes.push( wsmAttributes.split(" ") );  // modify the copy
  245.                 }
  246.               for( var i = 0; i < attributes.length; i++ )
  247.                 {
  248.                   dataObject[attributes[i]] = aElement.getAttribute( attributes[i] );
  249.                 }
  250.                 dataObject.localname = aElement.localName;
  251.               return dataObject;
  252.             }
  253.             return null;
  254.         },
  255.  
  256.     // <colorpicker>
  257.     set_Colorpicker:
  258.       function ( aElementID, aDataObject )
  259.         {
  260.           var element = wsm.contentArea.document.getElementById( aElementID );
  261.           // set all generic properties
  262.           wsm.generic_Set( element, aDataObject );
  263.           // set colorpicker specific properties
  264.           if( 'color' in aDataObject )
  265.             {
  266.               try {
  267.                 element.color = aDataObject.color;
  268.               }
  269.               catch (ex) {
  270.                 dump(aElementID +", ex: " + ex + "\n");
  271.               }
  272.             }
  273.         },
  274.  
  275.     get_Colorpicker:
  276.       function ( aElementID )
  277.         {
  278.           var element     = wsm.contentArea.document.getElementById( aElementID );
  279.           // retrieve all generic attributes
  280.           var dataObject  = wsm.generic_Get( element );
  281.           // retrieve all colorpicker specific attributes
  282.           if( dataObject )
  283.             {
  284.               dataObject.color = element.color;
  285.               return dataObject;
  286.             }
  287.           return null;
  288.         },
  289.  
  290.     // <menulist>
  291.     set_Menulist:
  292.       function ( aElementID, aDataObject )
  293.         {
  294.           var element = wsm.contentArea.document.getElementById( aElementID );
  295.           // set all generic properties
  296.           wsm.generic_Set( element, aDataObject );
  297.           // set menulist specific properties
  298.           if( 'value' in aDataObject )
  299.             {
  300.               try {
  301.                 element.value = aDataObject.value;
  302.               }
  303.               catch (ex) {
  304.                 dump(aElementID +", ex: " + ex + "\n");
  305.               }
  306.             }
  307.         },
  308.  
  309.     get_Menulist:
  310.       function ( aElementID )
  311.         {
  312.           var element     = wsm.contentArea.document.getElementById( aElementID );
  313.           // retrieve all generic attributes
  314.           var dataObject  = wsm.generic_Get( element );
  315.           // retrieve all menulist specific attributes
  316.           if( dataObject )
  317.             {
  318.               dataObject.value = element.getAttribute( "value" );
  319.               return dataObject;
  320.             }
  321.           return null;
  322.         },
  323.  
  324.     // <radiogroup>
  325.     set_Radiogroup:
  326.       function ( aElementID, aDataObject )
  327.         {
  328.  
  329.           var element = wsm.contentArea.document.getElementById( aElementID );
  330.           wsm.generic_Set( element, aDataObject );
  331.           if( 'value' in aDataObject )
  332.             {
  333.               element.value = aDataObject.value;
  334.             }
  335.           if( 'disabled' in aDataObject )
  336.             {
  337.               element.disabled = aDataObject.disabled;
  338.             }
  339.         },
  340.  
  341.     get_Radiogroup:
  342.       function ( aElementID )
  343.         {
  344.           var element = wsm.contentArea.document.getElementById( aElementID );
  345.           var dataObject = wsm.generic_Get( element );
  346.           if( dataObject )
  347.             {
  348.               dataObject.value = element.getAttribute( "value" );
  349.               return dataObject;
  350.             }
  351.           return null;
  352.         },
  353.  
  354.     // <textbox>
  355.     set_Textbox:
  356.       function ( aElementID, aDataObject )
  357.         {
  358.           var element = wsm.contentArea.document.getElementById( aElementID );
  359.           wsm.generic_Set( element, aDataObject );
  360.         },
  361.  
  362.     get_Textbox:
  363.       function ( aElementID )
  364.         {
  365.           var element = wsm.contentArea.document.getElementById( aElementID );
  366.           var dataObject = wsm.generic_Get( element );
  367.           if( dataObject )
  368.             {
  369.               dataObject.value = element.value;
  370.               return dataObject;
  371.             }
  372.           return null;
  373.         },
  374.  
  375.     // <checkbox>
  376.     set_Checkbox:
  377.       function ( aElementID, aDataObject )
  378.         {
  379.           var element = wsm.contentArea.document.getElementById( aElementID );
  380.           // Set generic properites. 
  381.           wsm.generic_Set( element, aDataObject );
  382.           // Handle reversed boolean values.
  383.           if ( "checked" in aDataObject && element.hasAttribute( "reversed" ) )
  384.             element.checked = !aDataObject.checked; 
  385.         },
  386.  
  387.     get_Checkbox:
  388.       function ( aElementID )
  389.         {
  390.           var element = wsm.contentArea.document.getElementById( aElementID );
  391.           var dataObject = wsm.generic_Get( element );
  392.           if( dataObject )
  393.             {
  394.               var checked = element.checked;
  395.               dataObject.checked = element.getAttribute("reversed") == "true" ? !checked : checked;
  396.               return dataObject;
  397.             }
  398.           return null;
  399.         },
  400.  
  401.     // <listitem>
  402.     set_Listitem:
  403.       function ( aElementID, aDataObject )
  404.         {
  405.           var element = wsm.contentArea.document.getElementById( aElementID );
  406.           wsm.generic_Set( element, aDataObject );
  407.           // Handle reversed boolean values.
  408.           if ( "checked" in aDataObject && element.hasAttribute( "reversed" ) )
  409.             element.checked = !aDataObject.checked; 
  410.         },
  411.  
  412.     get_Listitem:
  413.       function ( aElementID )
  414.         {
  415.           var element = wsm.contentArea.document.getElementById( aElementID );
  416.           var dataObject = wsm.generic_Get( element );
  417.           if( dataObject )
  418.             {
  419.               if( element.getAttribute("type") == "checkbox" )
  420.                 {
  421.                   var checked = element.checked;
  422.                   dataObject.checked = element.getAttribute("reversed") == "true" ? !checked : checked;
  423.                 }
  424.               return dataObject;
  425.             }
  426.           return null;
  427.         },
  428.  
  429.     // <data>
  430.     set_Data:
  431.       function ( aElementID, aDataObject )
  432.         {
  433.  
  434.           var element = wsm.contentArea.document.getElementById( aElementID );
  435.           wsm.generic_Set( element, aDataObject );
  436.           if( 'value' in aDataObject )
  437.             {
  438.               element.setAttribute("value", aDataObject.value);
  439.             }
  440.         },
  441.  
  442.     get_Data:
  443.       function ( aElementID )
  444.         {
  445.           var element = wsm.contentArea.document.getElementById( aElementID );
  446.           var dataObject = wsm.generic_Get( element );
  447.           if( dataObject )
  448.             {
  449.               dataObject.value = element.getAttribute( "value" );
  450.               return dataObject;
  451.             }
  452.           return null;
  453.         },
  454.  
  455.     // <default>
  456.     set_Default:
  457.       function ( aElementID, aDataObject )
  458.         {
  459.           var element = wsm.contentArea.document.getElementById( aElementID );
  460.           wsm.generic_Set( element, aDataObject );
  461.         },
  462.  
  463.     get_Default:
  464.       function ( aElementID )
  465.         {
  466.           var element = wsm.contentArea.document.getElementById( aElementID );
  467.           var dataObject = wsm.generic_Get( element );
  468.           return dataObject ? dataObject : null;
  469.         }
  470.   }
  471.  
  472.  
  473. // M:tHoF Greatest Hits Section (Append one line per edit):
  474. // it will be dark soon 
  475. // MANOS MADE ME PERMANENT! 
  476. // there is no way out of here
  477. // [The Master] not dead as you know it. He is with us always.
  478.  
  479.